<?php
class Shipping_Model_ShipType
{

	private $_table;
	public function __construct(){
		$this->_table = new Shipping_Model_DbTable_ShipType();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='st_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}


	public function delete($value,$field='st_id'){

		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);

		return $this->_table->delete($where);

	}


	public function getByValue($value,$field='st_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	public function getAllByValue($value,$field='st_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
        $order=array('st_code asc');
        $select->order($order);
		return $this->_table->getAdapter()->fetchAll($select);
	}
	public function getAll(){
		$select = $this->_table->getAdapter()->select();
		$table  = $this->_table->info('name');
		$select->from($table,'*');
		$select->where("1 = ?",1);
		return $this->_table->getAdapter()->fetchAll($select);
	}

    public function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["st_status"]) && $condition["st_status"]!=''){
            $select->where("st_status ='?'",$condition["st_status"]);
        }
		if(isset($condition["st_name_cn"]) && $condition["st_name_cn"]!=''){
            $select->where(" st_name_cn = ?",$condition["st_name_cn"]);
        }
        if(isset($condition["st_code"]) && $condition["st_code"]!=''){
            $select->where(" st_code =?",$condition["st_code"]);
        }
        if(isset($condition["st_class_code"]) && $condition["st_class_code"]!=''){
            $select->where(" st_class_code =?",$condition["st_class_code"]);
        }
        if(isset($condition["st_sort"]) && $condition["st_sort"]!=''){
            $select->where(" st_sort =? ",$condition["st_sort"]);
        }

        if(isset($condition["st_id"]) && $condition["st_id"]!=''){
            $select->where(" st_id <>?",$condition["st_id"]);
        }

        if('count(*)'==$type){
            //$sql = $select->__toString();
            return $this->_table->getAdapter()->fetchone($select);
        }else{
            if($orderby!='')
            {
                $orderbyArr= explode(',',$orderby);
                foreach($orderbyArr as $order)
                {
                    $select->order($order);
                }

            }
            if($pageSize>0 && $page>0)
            {

                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
		    return $this->_table->getAdapter()->fetchAll($select);
        }

	}
}